package com.wandousoushu.jiusen.ui.my

import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.text.TextUtils
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.wandousoushu.jiusen.MyApplication
import com.wandousoushu.jiusen.MyApplication.Companion.instance
import com.wandousoushu.jiusen.R
import com.wandousoushu.jiusen.bean.UserInfo
import com.wandousoushu.jiusen.bean.VipPay
import com.wandousoushu.jiusen.util.GsonUtils
import com.wandousoushu.jiusen.util.gone
import com.wandousoushu.jiusen.web.http.MainHttpUtils
import kotlinx.android.synthetic.main.activity_vip.*
import org.jetbrains.anko.sdk27.listeners.onClick
import org.json.JSONObject

class VipActivity : AppCompatActivity() {
    var vipDialog: VipOpenDialog? =null
    var isRenew=false;
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_vip)
        InitView()
        HttpGetPayRule()
    }

    fun InitView() {
        isRenew=intent.getBooleanExtra("isrenew",false)
        if(isRenew){
            vip_btn_open.setText(R.string.vip_open_renew)
        }else{
            vip_btn_open.setText(R.string.vip_open)
        }
        //vip_ll_back.onClick { finish() }
    }

    override fun onResume() {
        super.onResume()
        //HttpUserInfo()
        if (MyApplication.instance.isVip){
            mHandle.sendEmptyMessage(4)
        }
    }

    private fun HttpGetPayRule() {
        Thread(Runnable {
            try {
                val returnData = MainHttpUtils.getInstance().GetPayRule(instance.token)
                if (returnData == null) {
                    return@Runnable
                }
                val jsonObject = JSONObject(returnData)
                if (jsonObject != null && jsonObject.getInt("code") == 1) {
                    val data = jsonObject.getString("data")
                    if (TextUtils.isEmpty(data)) return@Runnable
                    val vipPay = GsonUtils.parseJObject(jsonObject.getString("data"), VipPay::class.java)
                    if (vipPay == null) return@Runnable
                    instance.AddPayRules(vipPay.cash_rule)
                    mHandle.sendEmptyMessage(3)
                } else {
                    val msg = Message.obtain()
                    msg.obj = jsonObject.getString("msg")
                    msg.what = 2
                    mHandle.sendMessage(msg)
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }).start()
    }

    private fun HttpUserInfo() {
        Thread(Runnable {
            try {
                val returnData = MainHttpUtils.getInstance().GetUserInfo(instance.token)
                if (returnData == null) {
                    return@Runnable
                }
                val jsonObject = JSONObject(returnData)
                if (jsonObject != null && jsonObject.getInt("code") == 1) {
                    val data = jsonObject.getString("data")
                    if (TextUtils.isEmpty(data)) {
                        return@Runnable
                    }
                    val userInfo = GsonUtils.parseJObject(jsonObject.getString("data"), UserInfo::class.java)
                    if (userInfo != null) {
                        MyApplication.instance.isVip = if (userInfo.is_vip == 1) true else false
                    }
                } else {
                    val msg = Message.obtain()
                    msg.obj = jsonObject.getString("msg")
                    msg.what = 2
                    mHandle.sendMessage(msg)
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }).start()
    }

    private val mHandle: Handler = object : Handler() {
        override fun handleMessage(msg: Message) {
            when (msg.what) {
                1 -> Toast.makeText(applicationContext, resources.getString(R.string.payrule_http_fail), Toast.LENGTH_SHORT).show()
                2 -> Toast.makeText(applicationContext, msg.obj.toString(), Toast.LENGTH_SHORT).show()
                3 -> vip_btn_open.onClick {
                    val activity = intent.getStringExtra("activity") ?: null
                    vipDialog=VipOpenDialog()
                    val bundle = Bundle()
                    bundle.putInt("isrenew", if (isRenew) 1 else 0)
                    vipDialog!!.setArguments(bundle);
                    vipDialog!!.show(supportFragmentManager, activity) }
                4 -> {
                    if(vipDialog!=null){
                        vipDialog!!.dismiss()
                        vip_btn_open.gone()
                    }
                }
            }
        }
    }
}